<template>
  <div>
    <search-form @search="getTableData" @reset="clearSearch">
      <el-col :span="8" :xs="24" v-show="$permiss[1374]">
        <el-form-item :label="$t('所属租户')">
          <el-select style="width: 100%" v-model="page.tenantId" @change="tenantChange">
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" >
        <el-form-item :label="$t('APP名称')">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1373]">
        <el-form-item :label="$t('客群名称')">
          <el-select style="width: 100%" v-model="page.guestGroupId">
            <el-option v-for="item in guestGroupList" :key="item.guestGroupId" :label="item.guestGroupName"
                       :value="item.guestGroupId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1614]">
        <el-form-item :label="$t('经理名称')">
          <el-select style="width: 100%"
                     v-model="page.managerId" clearable filterable @change="managerChange">
            <el-option v-for="item in managerList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1615]">
        <el-form-item :label="$t('催收员')">
          <el-select style="width: 100%" v-model="page.adminUserId" clearable filterable>
            <el-option v-for="item in collectionUser" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('日期')">
          <el-date-picker
              v-model="page.timeValue"
              style="width: 100%"
              type="daterange"
              range-separator="~"
              unlink-panels
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始时间')"
              :end-placeholder="$t('结束时间')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </search-form>
    <el-card shadow="never" style="margin-top: 10px">
      <el-tabs v-model="activeName" @tab-click="getTableData">
        <el-tab-pane label="订单业绩" name="order">
          <Table :table-column="orderTableColumn" :loading="loading" :table-data="tableData" :page="page"
                 @handleCurrentChange="handleCurrentChange"
                 @handleSizeChange="handleSizeChange"
                 max-height="700"/>
        </el-tab-pane>
        <el-tab-pane label="金额业绩" name="amount">
          <Table :table-column="amountTableColumn" :loading="loading" :table-data="tableData" :page="page"
                 @handleCurrentChange="handleCurrentChange"
                 @handleSizeChange="handleSizeChange"
                 max-height="700"/>
        </el-tab-pane>

      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import {selectProduct, selectTenant} from "@/api/system";
import {guestGroupBindList, guestGroupList, orderSplitAmountData, orderSplitOrderData} from "@/api/postLoan";
import {dateFormatSection, defaultTimeDate} from "@/utils/tools";

export default {
  name: "loanReportCollectionWeekly",
  data() {
    return {
      page: {
        timeValue: defaultTimeDate(7),
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      guestGroupList: [],
      exportsLoading: false,
      tenantList: [],
      managerList: [],
      collectionUser: [],
      tableData: [],
      amountTableColumn: [
        {label: this.$t('排名'), prop: 'rank', align: 'center', minWidth: 100, fixed: 'left'},
        {label: this.$t('客群'), prop: 'guestGroupName', align: 'center', minWidth: 100},
        {label: this.$t('系统账号'), prop: 'adminUserAcc', align: 'center', minWidth: 100},
        {label: this.$t('员工姓名'), prop: 'adminUserName', align: 'center', minWidth: 100},
        {label: this.$t('新增金额'), prop: 'newAmount', align: 'center', minWidth: 100},
        {label: this.$t('新增金额催回'), prop: 'newRecallAmount', align: 'center', minWidth: 100},
        {label: this.$t('新增金额催回率'), prop: 'newRecallAmountRate', align: 'center', minWidth: 120},
        {label: this.$t('总金额'), prop: 'stockAmount', align: 'center', minWidth: 100},
        {label: this.$t('总金额催回'), prop: 'stockRecallAmount', align: 'center', minWidth: 120},
        {label: this.$t('总金额催回率'), prop: 'stockRecallAmountRate', align: 'center', minWidth: 120},
      ],
      orderTableColumn: [
        {label: this.$t('排名'), prop: 'rank', align: 'center', minWidth: 100, fixed: 'left'},
        {label: this.$t('客群'), prop: 'guestGroupName', align: 'center', minWidth: 100},
        {label: this.$t('系统账号'), prop: 'adminUserAcc', align: 'center', minWidth: 100},
        {label: this.$t('员工姓名'), prop: 'adminUserName', align: 'center', minWidth: 100},
        {label: this.$t('新增订单数'), prop: 'newOrderNum', align: 'center', minWidth: 100},
        {label: this.$t('新增订单结清数'), prop: 'newRecallOrderNum', align: 'center', minWidth: 120},
        {label: this.$t('新增订单结清率'), prop: 'newRecallOrderNumRate', align: 'center', minWidth: 120},
        {label: this.$t('总订单数'), prop: 'stockOrderNum', align: 'center', minWidth: 100},
        {label: this.$t('总订单结清数'), prop: 'stockRecallOrderNum', align: 'center', minWidth: 120},
        {label: this.$t('总订单结清率'), prop: 'stockRecallOrderNumRate', align: 'center', minWidth: 120},
      ],
      activeName: 'order',
      loading: false,
      selectDate: null,
      pickerOptions: {
        onPick: ({maxDate, minDate}) => {
          this.selectDate = minDate.getTime()
          if (maxDate) {
            this.selectDate = null
          }
        },
        disabledDate: (time) => {
          // 最大可以选择前后31天时间
          if (this.selectDate !== null) {
            const dayNum = 30 * (24 * 60 * 60 * 1000)
            const minTime = this.selectDate - dayNum
            const maxTime = this.selectDate + dayNum
            return time.getTime() < minTime || time.getTime() > maxTime
          }
        },
      },
      productList: []
    }
  },
  async mounted() {
    const p2 = await selectTenant().then(res => {
      this.tenantList = res.data
      if (this.tenantList.length > 0) this.$set(this.page, 'tenantId', this.tenantList[0].adminUserId)
    })
    // 获取客群名称
    const p1 = await guestGroupList({
      pageNum: 1,
      pageSize: 1000,
      type: 1,
      tenantId: this.page.tenantId
    }).then(res => {
      const {list} = res.data
      this.guestGroupList = list || []
      if (this.guestGroupList.length > 0) this.$set(this.page, 'guestGroupId', this.guestGroupList[0].guestGroupId)
    })
    //  催收员
    this.$permiss[1615] && guestGroupBindList({
      pageSize: 100,
      adminRole: 7,
    }).then(res => {
      this.collectionUser = res.data.list
    })
    // 经理姓名
    this.$permiss[1614] && guestGroupBindList({
      adminRole: 6,
    }).then(res => {
      this.managerList = res.data.list
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
    Promise.all([p1, p2]).then(res => {
      this.getTableData()
    })

  },
  methods: {
    async getTableData() {
      this.loading = true
      const [dateStart, dateEnd] = dateFormatSection(this.page.timeValue)
      const params = {
        ...this.page,
        dateStart, dateEnd,
        timeValue: undefined,
      }
      const api = (params) => this.activeName === 'order' ? orderSplitOrderData(params) : orderSplitAmountData(params)
      const res = await api(params)
      if (res.code === 1) {
        const {list, total} = res.data
        if (list.list.length > 0) {
          this.tableData = list.list.map((item, index) => {
            item.rank = index + 1
            return item
          })
          this.page.total = list.total
          this.tableData.unshift({...total, rank: this.$t('合计')})

        } else {
          this.tableData = []
        }
        this.loading = false
      } else {
        this.loading = false
      }
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    tenantChange(value) {
      guestGroupList({
        pageNum: 1,
        pageSize: 1000,
        type: 1,
        tenantId: value
      }).then(res => {
        const {list} = res.data
        this.guestGroupList = list || []
      })

      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
      })
    },
    managerChange(value) {
      //  催收员
      guestGroupBindList({
        pageSize: 100,
        adminRole: 7,
        managerId: value
      }).then(res => {
        this.$set(this.page, 'adminUserId', null)
        this.collectionUser = res.data.list
      })
    },
    clearSearch() {
      // this.page = {
      //   pageNum: 1,
      //   pageSize: 10,
      //   total: 0
      // }
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>
